﻿namespace Blaze.Editor
{
    using System.IO;
    using Framework;
    using UnityEditor;
    using UnityEngine;

    /// <summary>
    /// Assets菜单项。
    /// </summary>
    public static class AssetsMenu
    {
        [MenuItem("Assets/Create/Blaze/Animation Curve Asset", false, 0)]
        public static void CreateAnimationCurve()
        {
            createAsset<AnimationCurveAsset>("New AnimationCurve");
        }

        private static void createAsset<T>(string name) where T : ScriptableObject
        {
            var dir = "Assets/";
            var selected = Selection.activeObject;
            if (selected != null)
            {
                var assetDir = AssetDatabase.GetAssetPath(selected.GetInstanceID());
                if (assetDir.Length > 0 && Directory.Exists(assetDir))
                    dir = assetDir + "/";
            }
            ScriptableObject asset = ScriptableObject.CreateInstance<T>();
            AssetDatabase.CreateAsset(asset, dir + name + ".asset");
            AssetDatabase.SaveAssets();
            EditorUtility.FocusProjectWindow();
            Selection.activeObject = asset;
        }
    }
}